﻿using Engine;
using Engine.Graphics;
using Game;
using GameEntitySystem;
using System;
using System.Collections.Generic;
using TemplatesDatabase;
using static Game.SubsystemCreatureSpawn;

namespace SAGhoul.Tartareosity
{
	public class SubsystemTartareosity : SubsystemWorld, IUpdateable
	{
		public SubsystemSAG m_subsystemSAG;
		public SubsystemWeather m_subsystemWeather;
		public List<Point3> m_originatePointList = []; //建筑群生成点
		public string[][] m_buildings;  //以区块为单位的建筑文本数组
		public int coordsXnum; //建筑群在X轴方向的区块数
		public int coordsYnum; //建筑群在Z轴方向的区块数

		public SubsystemWeather subsystemWeather;

		public bool IsCityGenerated;
		public override void Load(ValuesDictionary valuesDictionary)//加载世界时候的预设
		{
			base.Load(valuesDictionary);
			m_subsystemSAG = Project.FindSubsystem<SubsystemSAG>(true);
			m_subsystemWeather = Project.FindSubsystem<SubsystemWeather>(true);
			IsCityGenerated = valuesDictionary.GetValue("IsCityGenrated", false);
			subsystemWeather = Project.FindSubsystem<SubsystemWeather>(true);
			if (WorldType == WorldType.Tartareosity)
			{
				SubsystemGameInfo subsystemGameInfo = Project.FindSubsystem<SubsystemGameInfo>(true);
				m_subsystemTerrain.TerrainContentsGenerator = new TartareosityTerrainGenerator(m_subsystemTerrain);
				subsystemGameInfo.WorldSettings.TimeOfDayMode = TimeOfDayMode.Night;
				subsystemGameInfo.WorldSettings.EnvironmentBehaviorMode = EnvironmentBehaviorMode.Living;
				ChangeCreatureTypes();
			}
		}
		public override void Save(ValuesDictionary valuesDictionary)
		{
			base.Save(valuesDictionary);
			valuesDictionary.SetValue("IsCityGenrated", IsCityGenerated);
		}
		public override void OnEntityAdded(Entity entity)
		{
			base.OnEntityAdded(entity);
		}

		public new void Update(float dt)
		{
			base.Update(dt);
			if (WorldType == WorldType.Tartareosity)
			{
				foreach (var player in m_subsystemPlayers.ComponentPlayers)
				{
					player.ComponentFlu.m_fluDuration = 0f;
					player.ComponentFlu.m_coughDuration = 0f;//禁用感冒
					player.ComponentSickness.m_sicknessDuration = 0f;
					player.ComponentSickness.m_greenoutDuration = 0f;//禁用恶心
					player.ComponentVitalStats.Sleep = 1f;//禁用睡觉
					player.ComponentVitalStats.Stamina = 1f;//耐力保持满值
					player.ComponentVitalStats.Temperature = 12f;//温度适宜
					player.ComponentVitalStats.Wetness = 0f;//保持干燥
				}
				m_subsystemWeather.ManualPrecipitationEnd();//禁用降雨
			}
		}

		void ChangeCreatureTypes()
		{//更改自然刷怪
			SubsystemCreatureSpawn subsystemCreatureSpawn = Project.FindSubsystem<SubsystemCreatureSpawn>(true);
			subsystemCreatureSpawn.m_creatureTypes.Clear();
			subsystemCreatureSpawn.m_creatureTypes.Add(new CreatureType("Tartareosity Ghouls", SpawnLocationType.Surface, randomSpawn: true, constantSpawn: true)
			{
				SpawnSuitabilityFunction = (creatureType, point) => 10f,
				SpawnFunction = delegate (CreatureType creatureType, Point3 point)
				{
					int totalCount = 0;
					string[] types = [
						"SAGhoul_Fast_06", "SAGhoul_Fast_07", "SAGhoul_Weak_06", "SAGhoul_Weak_07",
							"SAGhoul_Strong_01", "SAGhoul_Strong_02", "SAGhoul_Strong_03", "SAGhoul_Strong_04",
							"SAGhoul_Strong_05", "SAGhoul_Strong_06", "SAGhoul_Strong_07", "SAGhoul_Strong_08"
					];
					int[] counts = [2, 2, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2];
					for (int i = 0; i < types.Length; i++)
					{
						if (subsystemCreatureSpawn.m_random.Float(0f, 1f) < 0.1f)
							totalCount += subsystemCreatureSpawn.SpawnCreatures(creatureType, types[i], point, counts[i]).Count;
					}
					// 动物
					(string Type, float Chance, int Count)[] eliteConfigs = [
						("SAGhoul_Bear", 0.4f, 1),
						("SAGhoul_Monster", 0.4f, 1),
						("SAGhoul_Fatt", 0.4f, 1),
						("SAGhoul_Boar", 0.4f, 1),
						("SAGhoul_Ray", 0.25f, 3),
						("SAGhoul_Bird", 0.2f, 3)
					];
					for (int i = 0; i < eliteConfigs.Length; i++)
					{
						var (Type, Chance, Count) = eliteConfigs[i];
						if (subsystemCreatureSpawn.m_random.Float(0f, 1f) < Chance)
						{
							totalCount += subsystemCreatureSpawn.SpawnCreatures(creatureType, Type, point, Count).Count;
						}
					}
					if (totalCount == 0)// 确保至少生成一个生物
						totalCount += subsystemCreatureSpawn.SpawnCreatures(creatureType, "SAGhoul_Bird", point, 1).Count;
					return totalCount;
				}
			});
			//subsystemCreatureSpawn.m_creatureTypes.Add(new SubsystemCreatureSpawn.CreatureType("Seagull", SpawnLocationType.Surface, randomSpawn: true, constantSpawn: false)
			//{
			//	SpawnSuitabilityFunction = delegate (SubsystemCreatureSpawn.CreatureType creatureType, Point3 point)
			//	{
			//		float oceanShoreDistance = base.m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z);
			//		return oceanShoreDistance < 8f ? 5f : 0f;
			//	},
			//	SpawnFunction = (creatureType, point) => subsystemCreatureSpawn.SpawnCreatures(creatureType, "Seagull", point, 1).Count
			//});
			//subsystemCreatureSpawn.m_creatureTypes.Add(new SubsystemCreatureSpawn.CreatureType("White Tigers", SpawnLocationType.Surface, randomSpawn: true, constantSpawn: false)
			//{
			//	SpawnSuitabilityFunction = delegate (SubsystemCreatureSpawn.CreatureType creatureType, Point3 point)
			//	{
			//		float oceanShoreDistance = base.m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z);
			//		return oceanShoreDistance > 8f ? 5f : 0f;
			//	},
			//	SpawnFunction = (creatureType, point) => subsystemCreatureSpawn.SpawnCreatures(creatureType, "Tiger_White", point, 1).Count
			//});
			//subsystemCreatureSpawn.m_creatureTypes.Add(new SubsystemCreatureSpawn.CreatureType("White Bull", SpawnLocationType.Surface, randomSpawn: true, constantSpawn: false)
			//{
			//	SpawnSuitabilityFunction = delegate (SubsystemCreatureSpawn.CreatureType creatureType, Point3 point)
			//	{
			//		float oceanShoreDistance = base.m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z);
			//		return oceanShoreDistance > 8f ? 5f : 0f;
			//	},
			//	SpawnFunction = (creatureType, point) => subsystemCreatureSpawn.SpawnCreatures(creatureType, "Bull_White", point, 1).Count
			//});
			//subsystemCreatureSpawn.m_creatureTypes.Add(new SubsystemCreatureSpawn.CreatureType("Polar Bears", SpawnLocationType.Surface, randomSpawn: true, constantSpawn: false)
			//{
			//	SpawnSuitabilityFunction = delegate (SubsystemCreatureSpawn.CreatureType creatureType, Point3 point)
			//	{
			//		float oceanShoreDistance = base.m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z);
			//		return oceanShoreDistance > 8f ? 5f : 0f;
			//	},
			//	SpawnFunction = (creatureType, point) => subsystemCreatureSpawn.SpawnCreatures(creatureType, "Bear_Polar", point, 1).Count
			//});
			//subsystemCreatureSpawn.m_creatureTypes.Add(new SubsystemCreatureSpawn.CreatureType("White Horse", SpawnLocationType.Surface, randomSpawn: true, constantSpawn: false)
			//{
			//	SpawnSuitabilityFunction = delegate (SubsystemCreatureSpawn.CreatureType creatureType, Point3 point)
			//	{
			//		float oceanShoreDistance = base.m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z);
			//		return oceanShoreDistance > 8f ? 5f : 0f;
			//	},
			//	SpawnFunction = (creatureType, point) => subsystemCreatureSpawn.SpawnCreatures(creatureType, "Horse_White", point, 1).Count
			//});
		}
	}
}
